<?php

namespace App\Http\Controllers\Business\Shop\V1\DoubleCalendar;

use App\Models\Shop;
use App\Models\ShopEvent;
use App\Http\Controllers\Business\Shop\V1\ShopLog\ShopLog;

class DoubleCalendar {
    public static function setDoubleCalendar($shopId,$event = [], $shopUserId='') {
        if (!$shopId || !is_array($event) || !$event) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $data = [];
        $shop = Shop::where('id', $shopId)->first();
        if(!$shop) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        if($shop->status == '0') {
            return ['status'=>'failed', 'message'=>'门店已下架，请联系客服'];
        }
        $nextEvent = $shop->nextevent;
        $tmp = '2020-12-31';
        foreach ($event as $v) {
            if($nextEvent != '0000-00-00') {
                if ($v > date('Y-m-d') && $v < $nextEvent) {
                    $shop->nextevent = $v;
                    $shop->save();
                }
            } else {
                if($tmp>$v) {
                    $tmp = $v;
                }
            }
            array_push($data,array('shopid'=>$shopId,'event'=>$v));
        }
        if($nextEvent == '0000-00-00') {
            $shop->nextevent = $tmp;
            $shop->save();
        }
        $shopEvents = ShopEvent::where('shopid',$shopId)->get();
        $shopEventStr = '';
        $reason = '';
        $shopEventArr = array();
        $eventArrTrim = array();
        foreach($event as $key=>$value){
            $eventArrTrim[$key] = trim($value,' ');
        }
        foreach($shopEvents as $key=>$value){
            $shopEventArr[$key] = $value['event'];
            $shopEventStr .= $value['event'];
        }
        $eventDiff = array_merge(array_diff($eventArrTrim,$shopEventArr),array_diff($shopEventArr,$eventArrTrim));
        if(!empty($eventDiff)){
            if(count($eventDiff) >20){
                $reason .= '商家翻贝日期发生改变;';
            }else{
                $reason .= '商家翻贝日期更改了"'.implode(',',$eventDiff).'";';
            }
        }
        $reason = rtrim($reason, ";") ;
        $result = ShopLog::writeShopLog($shopUserId, $shopId, 'shop_set_doublecalendar', 0, $reason, '');

        ShopEvent::where('shopid',$shopId)->delete();
        ShopEvent::insert($data);

        return ['status'=>'success','message'=>'添加成功！'];
    }
    public static function getDoubleCalendar($shopid) {
        if(!$shopid) {
            return ['status'=>'failed', 'message'=>'参数错误！'];
        }
        $shopEvents = ShopEvent::where('shopid', $shopid)->orderBy('event', 'asc')->select('event')->get();
        return ['status'=>'success', 'data'=>$shopEvents];
    }
    public static function nightShow($shopid,$type) {
        if (!$shopid || ($type != '0' && $type != '1')) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $forbidNightEvent = Shop::where('id', $shopid)->pluck('forbidnightevent')->first();
        if($forbidNightEvent != 0) {
            return ['status' => 'failed', 'message'=>'请联系客服'];
        }
        if($type=='0') {
            Shop::where('id', $shopid)->update(array('nightevent' => 0));
        } elseif($type == '1') {
            //需要判断是不是晚上九点，到凌晨3点之前打开的，最近一周有没有翻倍日
            $hour = date('H');
            if($hour >=3 && $hour <21) {
                return ['status'=>'failed', 'message'=>'夜场活动需要在21:00-02:59之间才能开启'];
            }
            $eventCount = ShopEvent::where('shopid', $shopid)->where('event', '>=', date('Y-m-d'))
                ->where('event', '<=', date('Y-m-d', strtotime("+7days")))->get()->count();
            if($eventCount<1) {
                return ['status'=>'failed', 'message'=>'开启夜场活动需要最近一周内设置了翻倍日'];
            }
            Shop::where('id', $shopid)->update(['nightevent' => 1]);
        } else {
            return ['status'=>'failed', 'message'=>'类型错误'];
        }
        return ['status'=>'success','message'=>'设置成功！'];
    }
}